<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
//生成验证码
    public function captcha(){
        $cfg = array(
            'fontSize'  =>  18,              // 验证码字体大小(px)
            'useCurve'  =>  false,            // 是否画混淆曲线
            'useNoise'  =>  false,            // 是否添加杂点
            'length'    =>  4,               // 验证码位数
            'imageH'	=>	48,
            'imageW'	=>	120,
            'bg'        =>  array(255,255,255),
            'fontttf'   =>  '4.ttf',// 验证码字体，不设置随机获取
        );
            $verify=new\Think\Verify($cfg);
            $verify->entry();

    }
    public function checklogin(){
        $post=I('post.');
        //验证验证码
        $verify=new\Think\Verify();
        $result=$verify->check($post['capecha']);
       if($result==false){
          $this->error("您输入的验证码错误");
       }
        else{
             $model=M("admin");
            $data=$model->where("aname=".$post["aname"] and "apwd=".$post["apwd"] )->find();
           if($data){
               session('aid',$data["aid"]);
               session('aname',$data["aname"]);
               session('isadministrators',$data["isadministrators"]);
               $this->success("登录成功",index,3);
           }
           else{
               $this->error("用户或者密码错误。。",login,3);;
           }


        }

    }


//展示登录页面
    public function  login(){
        $this->display("login");

    }
    //展示前台页面
    public function index()
{

    if(session('?aname')){
        $this->display("index");
    }
    else{
        $this->error("请登录",login,3);
    }

}
    //查看品牌表的内容
    public function brand(){
        $model=M('brand');
//        $pd=$model->select();
//        $this->assign("da",$pd);
//        $this->display("brand");
        $pagesize=1;
        $count=$model->count();
        $Page= new \Think\Page($count,$pagesize);
        $show  = $Page->show();
        $p=intval(I('get.p'));
        $data = $model->page($p,$pagesize)->select();
        return array('pageStr'=>$show,'data'=>$data);
        $this->display("brand");
    }
    //进行批量删除
    public function deltebrand(){
             $id=$_POST["ids"];
        $ids = substr($id,0,strlen($id)-1);
        $strid='bid in ('.$ids.')';
//        var_dump($strid);
        $md=M('brand');
     $lf=   $md->where($strid)->delete();

        if($lf>0){
            echo "1";
        }
        else{
            echo"删除失败";
        }
//        var_dump($lf);
//       $jdata= $md->select();
//$mydata=json_decode($jdata);
//        echo"$mydata";


    }
    //排序
//    public function  paixu(){
//        $post=I('post.');
//        var_dump($post);
//
//    }

//关键字品牌查询
public function seachbrand(){
      $post=$_POST["keywords"];
      $pd=M('brand');
     if($post!=null){
//         $data=$post;
//         $whereb["bid"]=$data;
//         $whereb["bname"]=array('like','%'.$data.'%');
//         $whereb["btel"]=array('like','%'.$data.'%');
       $data=  $pd->where("bid= '".$post." '   or  bname like '%".$post.  "%' or btel like '%".$post."%' or   bname= ' ".$post ."'" )->select();
         $this->assign("ll",$data);
         $this->display();
     }




}

    //删除单条
    public  function deltebone(){
        $bid=$_GET["bid"];
        $pd=M('brand');
        $pd->where("bid=".$bid)->delete();
        $mf=new IndexController();
        $mf->brand();

    }
    //展示添加品牌页面
    public function addbrand(){
        $this->display();
    }
//执行添加功能
   public function addgbrand(){
       //开始添加产品
           $config = array(
               'maxSize' => 3145728,                             // 设置附件上传大小，默认3M
               'exts' => array('jpg', 'gif', 'png', 'jpeg'),  // 设置附件上传类型
               'rootPath' => './',                                // 设置根路径
               'savePath' => 'Public/Upload/image/'                      // 设置附件上传目录
           );
           $upload = new\Think\Upload($config);
           // 上传文件
           $info = $upload->upload();
           foreach ($info as $file) {
               $_POST['bimages']= $file['savepath'] . $file['savename'];
               $_POST["bthum"]=$file['savepath'].'thumb_' . $file['savename'];
           }
       //制作缩略图
       $image=new\Think\Image();
       $image->open('./'.$_POST['bimages']);
       $image->thumb(150,150);
       $image->save('./'.$_POST["bthum"]);
        $pd=M('brand');
         $pd->create();
         $pd->add();
       $mf=new IndexController();
       $mf->brand();
   }

    //点击修改得到当前品牌的
    public function  getonebrand(){
        $bid=$_GET["bid"];
        $md=M('brand');
        $data=  $md->where("bid=".$bid)->find();
        $this->assign("kl",$data);
        $this->display();

    }
    public function updatabrand(){
        $config = array(
            'maxSize' => 3145728,                             // 设置附件上传大小，默认3M
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),  // 设置附件上传类型
            'rootPath' => './',                                // 设置根路径
            'savePath' => 'Public/Upload/image/'                      // 设置附件上传目录
        );
        $upload = new\Think\Upload($config);
        // 上传文件
        $info = $upload->upload();
        foreach ($info as $file) {
            $_POST['bimages']= $file['savepath'] . $file['savename'];
            $_POST["bthum"]=$file['savepath'].'thumb_' . $file['savename'];
        }
        //制作缩略图
        $image=new\Think\Image();
        $image->open('./'.$_POST['bimages']);
        $image->thumb(150,150);
        $image->save('./'.$_POST["bthum"]);
        $pd=M('brand');
        $pd->create();
        $pd->save();
        $mf=new IndexController();
        $mf->brand();
    }
    //产品类型
    public function  typegoods(){
        $pd=M("typegoods");
       $data=   $pd->select();
      $this->assign("da",$data);
    $this->display("typegoods");
    }
    //删除类型
    public function deltetype(){
        $tid=$_GET["tid"];
        $pd=M("typegoods");
        $pd->where("tid=".$tid)->delete();
        $pf=new IndexController();
        $pf->typegoods();

    }
//展示添加类型
public function addtype(){
$this->display("addtype");
}
    //进行了类型添加操作
    public function  addtypesgood(){
        $pd=M('typegoods');
        $pd->create();
        $pd->add();
        $pf=new IndexController();
        $pf->typegoods();

    }
    //修改类型
    public function gettype(){
        $tid=$_GET["tid"];
        $pm=M('typegoods') ;
        $data=$pm->where("tid=".$tid)->find();
        $this->assign("ll",$data);
        $this->display();

    }
    //修改类型
    public function updatatype(){
        $pd=M('typegoods');
        $pd->create();
        $pd->save();
        $pf=new IndexController();
        $pf->typegoods();


    }
    //批量删除类型
    public function deletestypes(){
        $id=$_POST["ids"];
        $ids = substr($id,0,strlen($id)-1);
        $strid='tid in ('.$ids.')';
//        var_dump($strid);
        $md=M('typegoods');
        $lf=   $md->where($strid)->delete();

        if($lf>0){
            echo "1";
        }
        else{
            echo"删除失败";
        }
    }
    //查看尺码
    public function  chimaguanli(){
         $pd=M('sizesgood');
         $datd= $pd->select();
         $this->assign("da",$datd);
        $this->display("chimaguanli");
    }
    //增加尺码
    public function addchima(){
        $this->display();
    }
    //进行尺码增加操作
    public function addchimacz(){
        $pd=M('sizesgood');
        $pd->create();
        $pd->add();
        $pm=new IndexController();
        $pm->chimaguanli();

    }
//  删除尺码
public  function deleteonechima(){
    $sid=$_GET["sid"];
    $pd=M('sizesgood');
  $pd->where("sid=".$sid)->delete();
    $pm=new IndexController();
    $pm->chimaguanli();
}
    //得到尺码
    public function getonechima(){
        $sid=$_GET["sid"];
        $pd=M('sizesgood');
       $data= $pd->where("sid=".$sid)->find();
      $this->assign("ll",$data);
     $this->display();
    }
    //更新尺码
public function updatachima(){
    $pd=M('sizesgood');
    $pd->create();
    $pd->save();
    $pm=new IndexController();
    $pm->chimaguanli();
}
    //批量删除尺码
    public function pilichima(){
        $id=$_POST["ids"];
        $ids = substr($id,0,strlen($id)-1);
        $strid='sid in ('.$ids.')';
//        var_dump($strid);
        $md=M('sizesgood');
        $lf=   $md->where($strid)->delete();

        if($lf>0){
            echo "1";
        }
        else{
            echo"删除失败";
        }
    }
    //查看颜色管理
    public function colorguli(){
        $pd=M('colorgood');
       $data= $pd->select();
       $this->assign("da",$data);
       $this->display("colorguli");
    }
    //删除颜色
    public function deletecolone(){
        $coid=$_GET["coid"];
        $pd=M('colorgood');
        $pd->where("coid=".$coid)->delete();
        $pf=new IndexController();
        $pf->colorguli();
    }
//显示颜色增加页面
public  function  addcolorpage(){
$this->display();
}
    //添加颜色
    public function addcolor(){
        $pd=M('colorgood');
        $pd->create();
        $pd->add();
        $pm=new IndexController();
        $pm->colorguli();

    }
    //得到颜色
    public function getcololone(){

        $coid=$_GET["coid"];
        $pd=M('colorgood');
        $data=$pd->where("coid=".$coid)->find();
        $this->assign("ll",$data);
        $this->display();
    }
//   更新颜色
public  function  updatacolor(){

    $pd=M('colorgood');
    $pd->create();
    $pd->save();
    $pm=new IndexController();
    $pm->colorguli();
}
    //批量删除颜色
    public function piliyanse(){
        $id=$_POST["ids"];
        $ids = substr($id,0,strlen($id)-1);
        $strid='coid in ('.$ids.')';
//        var_dump($strid);
        $md=M('colorgood');
        $lf=   $md->where($strid)->delete();
        if($lf>0){
            echo "1";
        }
        else{
            echo"删除失败";
        }

    }
    //展示商品
    public function  showgoods(){
       $pd=M('goodsss');
       $data= $pd->select();
       $this->assign("da",$data);
       $this->display("showgoods");
    }
    //展示添加商品
    public function addgoods(){
//查出品牌
        $model=M('brand');
        $pd=$model->select();
        $this->assign("da",$pd);
        //查询颜色
        $pd=M('colorgood');
        $cd=$pd->select();
        $this->assign("ll",$cd);
        //查询尺码
        $pf=M('sizesgood');
        $mm=$pf->select();
        $this->assign("kk",$mm);
//查询类型
        $lx=M('typegoods');
        $lk=$lx->select();
         $this->assign("qa",$lk);
        $this->display("addgoods");
    }

//开始添加
public function   addgoood(){

    $config = array(
        'maxSize' => 3145728,                             // 设置附件上传大小，默认3M
        'exts' => array('jpg', 'gif', 'png', 'jpeg'),  // 设置附件上传类型
        'rootPath' => './',                                // 设置根路径
        'savePath' => 'Public/Upload/image/' ,
        // 设置附件上传目录
    );
    $upload = new\Think\Upload($config);
    // 上传文件
    $info = $upload->upload();
    foreach ($info as $file) {
        $_POST['gimages']= $file['savepath'] . $file['savename'];
        $_POST['xijieimages1']=$file['savepath'] . $file['savename'];
        $_POST['xijieimages2']=$file['savepath'] . $file['savename'];
        $_POST['xijieimages3']=$file['savepath'] . $file['savename'];
        $_POST['xijieimages4']=$file['savepath'] . $file['savename'];
    }
     $pd=M('goodsss');
     $pd->create();
     $pd->add();
    $mm=new IndexController();
    $mm->showgoods();
}
    public function showxijietu(){
        $gid=$_GET["gid"];
        $pd=M('goods');
        $data= $pd->where("gid=".$gid)->find();
        $this->assign("ll",$data);
        $this->display();
    }

    public  function  deletegood(){
        $gid=$_GET["gid"];
        $spl=M('goods');
        $spl->where("gid=".$gid)->delete();
        $cvc=new IndexController();
        $cvc->showgoods();
    }
    public function  updatagood(){
        $gid=$_GET["gid"];
        $spl=M('goods');
        $mf=$spl->where("gid=".$gid)->find();
        $this->assign("hh",$mf);
        $this->display();
    }







}